package cn.yun.web.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/*
演示Request对象获取请求头中user-agent的数据
 Enumeration<>  getHeaderNames():获取请求头的所有名称 返回Enumeration对象
   String getHeader(String name):获取请求头中指定名称的数据

Enumeration:类似于迭代器
 方法: hasMoreElements():测试此枚举是否包含更多元素
          nextElement():如果此枚举对象至少有一个要提供的元素,返回此枚举的下一个元素

*/

@WebServlet("/requestDemo3")
public class RequestDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求头中 user-agent 数据
        String agent = request.getHeader("user-agent");
        //判断agent的浏览器信息
        if (agent.contains("Chrome")) {
            //谷歌浏览器
            System.out.println("谷歌来了....");
        } else if (agent.contains("Firefox")) {
            //火狐浏览器
            System.out.println("火狐来了....");
        }

    }

}

